package com.elvende.web.client.presenter.life;

import com.elvende.web.client.dto.AffortableHousing;
import com.elvende.web.client.presenter.Presenter;
import com.elvende.web.client.service.LifeServiceAsync;
import com.elvende.web.client.service.life.HousingServiceAsync;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
public class HousingPresenter implements Presenter {
	 private final HousingServiceAsync rpcService;
	  private final HandlerManager eventBus;
	  private final Display display;
	
	 public HousingPresenter(HousingServiceAsync rpcService, HandlerManager eventBus, Display view) {
		    this.rpcService = rpcService;
		    this.eventBus = eventBus;
		    this.display = view;
	 }
	 
	public interface Display {
		void setAffortableHousing(AffortableHousing[] housingData);		
		HasClickHandlers getList();
		Widget asWidget();
	}

	public void bind() {
		fetchAffortableHousing();
	}
	
   private void fetchAffortableHousing() {
	   rpcService.listAffortableHousing(new AsyncCallback<AffortableHousing[]>(){
		   public void onSuccess(AffortableHousing[] result) {	
			   display.setAffortableHousing(result);
		   }
		   public void onFailure(Throwable caught) {			   
		   }
	   });	   
   }

	
	public void go(HasWidgets container) {
	    bind();
	    container.clear();
	    container.add(display.asWidget());
	}
}
